package com.xw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xw.common.Constants;
import com.xw.common.TokenHandler;
import com.xw.dao.SubjectCategoryMapper;
import com.xw.entity.SubjectCategory;
import com.xw.entity.User;
import com.xw.exp.ServiceException;
import com.xw.manger.AliyunManger;
import com.xw.req.PageRequest;
import com.xw.service.SubjectCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 15536
 * @description 针对表【subject_category】的数据库操作Service实现
 * @createDate 2024-12-05 20:24:46
 */
@Service
public class SubjectCategoryServiceImpl extends ServiceImpl<SubjectCategoryMapper, SubjectCategory> implements SubjectCategoryService {

    @Resource
    private TokenHandler tokenHandler;

    @Override
    public void insertSubjectCategory(SubjectCategory subjectCategory) {
        User userInfo = tokenHandler.getUserInfo();
        if (userInfo == null) {
            throw new ServiceException("403", "用户未登录");
        }
        subjectCategory.setCreateUser(userInfo.getId());
        subjectCategory.setCreateTime(new Date());
        save(subjectCategory);
    }

    @Override
    public void updateSubjectCategory(SubjectCategory subjectCategory) {
        updateById(subjectCategory);
    }

    @Override
    public void deleteSubjectCategory(Long id) {
        removeById(id);

    }

    @Override
    public List<SubjectCategory> getAllSubjectCategoryList() {
        return list();
    }

    @Override
    public PageInfo<SubjectCategory> page(PageRequest<SubjectCategory> pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<SubjectCategory> categories = list(
                new LambdaQueryWrapper<SubjectCategory>()
                        .like(SubjectCategory::getCategoryName, pageRequest.getQ().getCategoryName())
        );
        PageInfo<SubjectCategory> pageInfo = new PageInfo<>(categories);
        return pageInfo;
    }

    @Override
    public void deleteBatchByIds(List<Integer> ids) {
        removeBatchByIds(ids);
    }
}




